/**
 * File  : PlayThread.cc
 * Time  : 2014-12-22 10:11
 * Author: 
 * Ref   : 
 */
#include "WorkThreads.h"
#include "./log/Log_Def.h"
#include "./play/play_data.h"
#include <iostream>
#include <sstream>
#include "./video/Video.h"
#include "./picture/Picture.h"
#include <fstream>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <unistd.h>
#include "ThreadMutex.h"
using namespace std;

int PlayThread::xmldecoder(){
		xmlDocPtr doc=NULL;           
    xmlNodePtr curNode;      
    xmlChar *szKey;        
    const char *szDocName = "./program/1.xml";
    int N=0;
    int i=0;

		while(doc==NULL){
			mutex_play_.Lock();
    	doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); 
			mutex_play_.Unlock();
			
    } 
    curNode = xmlDocGetRootElement(doc); 
    if (NULL == curNode)
    { 
       fprintf(stderr,"empty document\n"); 
       xmlFreeDoc(doc); 
       return -1; 
    } 
    if (xmlStrcmp(curNode->name, BAD_CAST "root")) 
    {
       fprintf(stderr,"document of the wrong type, root node != root"); 
       xmlFreeDoc(doc); 
       return -1; 
    } 
    curNode = curNode->xmlChildrenNode;
    curNode = curNode->next;
    szKey=xmlNodeGetContent(curNode);
    N=atoi(reinterpret_cast<const char *>(szKey));
    play_data *data=new play_data[N];
    while(curNode->next!= NULL) 
    {
       curNode = curNode->next;
      if (xmlHasProp(curNode,BAD_CAST "mediatype"))
       {
           data[i].mediatype=atoi(reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "mediatype")));
       }

      if (xmlHasProp(curNode,BAD_CAST "location"))
       {
           data[i].location=reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "location"));
       }

      if (xmlHasProp(curNode,BAD_CAST "playtime"))
       {
           data[i].playtime=atoi(reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "playtime")));
       }
      if (xmlHasProp(curNode,BAD_CAST "insertmode"))
       {
           data[i].insertmode=atoi(reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "insertmode")));
       }

       i++; 
		}

  for(i=0;i<N;i++)
  {  
    event_queue_->AddEvent(data[i]);
  }
  xmlFreeDoc(doc);
  return 0; 
}


void PlayThread::Run ()  {
  LogType log_type_;
  ostringstream oss;
  LOG_INFO(LOG_LEVEL_INFO) << "PLAY LOG ## " << "this is play starting";
  LOG_ADD;

  sleep(3);
	cout<<"playing thread going to work"<<endl;

  EventQueue<struct play_data> *play_queue_t=new EventQueue<struct play_data>;
  EventQueue<struct play_data> *insert_queue_t= new EventQueue<struct play_data>;

  for (;;) {
		xmldecoder();
    while(!event_queue_->EventQueue_Isempty())
    {   
      Type<play_data> *tmp = event_queue_->GetEvent();
      if(!tmp->opcode().insertmode){
        play_queue_t->AddEvent(tmp->opcode());
      }
      else if(tmp->opcode().insertmode){
        insert_queue_t->AddEvent(tmp->opcode());
      }
    }
    while((event_queue_->EventQueue_Isempty()) && \
        (!play_queue_t->EventQueue_Isempty()||!insert_queue_t->EventQueue_Isempty()))
    {
      
      Type<play_data> *tmp1;

      if(!insert_queue_t->EventQueue_Isempty()){
        tmp1 = insert_queue_t->GetEvent();
      }
      else if(!play_queue_t->EventQueue_Isempty()){
        tmp1 = play_queue_t->GetEvent();
      }
      
      LOG_INFO(LOG_LEVEL_INFO) << "PLAY LOG ## " << "PLAY media is " \
                               <<tmp1->opcode().location;
      LOG_ADD;
      switch(tmp1->opcode().mediatype)
      {
        case 0: {
                  settime(tmp1->opcode().playtime);
                  Picture picture1(tmp1->opcode().location,600,600);
                 // picture1.picture_display();
                  break;
                }
        case 1: { 
                  Video video(tmp1->opcode().location,600,600);
                //  video.video_display();
                  break;
                }
       default:break;
      }
      string location = "rm -rf "+tmp1->opcode().location;
      const char* loc = location.c_str();
      if(tmp1->opcode().insertmode){
        system(loc); 
        LOG_INFO(LOG_LEVEL_INFO) << "PLAY LOG ## " << "delete the finished insert program ## " \
                                 <<tmp1->opcode().location;
        LOG_ADD;
      }
     
    }


    sleep(1);
  }

    delete play_queue_t;
		delete insert_queue_t;

}

